package top.humbleyuan.genericity.datastructure;

/**
 * @Author HumbleYuan
 * @Date 2019/12/9 15:13
 * @Des 基于自定义链表实现堆栈
 */
public class LinkedListToStack<T> {

    /**
     * 末端哨兵，用来判断堆栈是否空
     * 类似于链表的头结点
     */
    public Node<T> root = new Node<T>();

    private static class Node<F> {
        private F item;
        private Node<F> next;
        boolean end() {
            return item == null && next == null;    // 判断是否为空
        }

        public Node() {
            super();
        }

        public Node (F i, Node<F> n) {
            item = i;
            next = n;
        }

        public F getItem() {
            return item;
        }

        public void setItem(F item) {
            this.item = item;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }


    public void push(T n) {
        root = new Node<>(n, root);
    }

    public T pop() {
        T top = root.item;
        if( !root.end() ) {
            root = root.next;
        }

        return top;
    }

}
